/* 
 * File:   Hash_Shingles.cpp
 * Author: fliaali
 * 
 * Created on 4 de octubre de 2013, 15:59
 */

#include "Hashs.h"
#include "string.h"


Hashs::Hashs() {  
}

Hashs::~Hashs() {
}

void Clear2(char* palabra, int l){
    for (int i=0;i<l;i++){
        palabra[i]='\0';
    }
}

char* Hashs::F_Hashing_Shingle(char* shingle){
    const char* Cods[] = {"00000","00001","00010","00011","00100","00101","00110","00111","01000","01001",
                          "01010","01011","01100","01101","01110","01111","10000","10001","10010","10011",
                          "10100","10101","10110","10111","11000","11001"};
    char letra;
    char* codigo_letra;
    char* codigo = (char*)malloc(sizeof(char)*32);
    char* salida = (char*)malloc(sizeof(char)*4);
    Clear2(codigo,32);
    Clear2(salida,4);
    for(int i=0;i < strlen(shingle);i++){
        letra = shingle[i];
        letra = letra - 97;
        codigo_letra = (char*)Cods[letra];
        strcat(codigo,codigo_letra);
    }
    strcat(codigo,"00");
    int ascii = 0;
    int indice = 0;
    for(int i=0;i<4;i++){
        for(int k=7;k>-1;k--){
            if(codigo[indice]=='1'){
                ascii = ascii + pow(2,k);
            }
            indice++;
        }
        strcat(salida,(char*)&ascii);
        ascii=0;
    }
    free(codigo);
    return salida;
}

